package Brand;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class BrandTest {



    @Test
    public void Test() throws Exception {


        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();

        String sql = "select * from tb_brand";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();

        Brands brands;
        List<Brands> l = new ArrayList<>();
        while(rs.next()){

            int id = rs.getInt("id");
            String brandName = rs.getString("brand_name");
            String companyName = rs.getString("company_name");
            int ordered = rs.getInt("ordered");
            String description = rs.getString("description");
            int status = rs.getInt("status");

            brands = new Brands();
            brands.setId(id);
            brands.setBrand_name(brandName);
            brands.setCompany_name(companyName);
            brands.setDescription(description);
            brands.setOrdered(ordered);
            brands.setStatus(status);


            l.add(brands);
        }

        rs.close();
        pstmt.close();
        conn.close();

        System.out.println(l);

    }

    @Test
    public void TestAdd() throws Exception {

        String brandName = "theshy";
        String companyName = "WBK";
        int ordered = 1;
        String description = "go ahead";
        int status = 1;

        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();

        String sql = "INSERT INTO tb_brand(brand_name,company_name,ordered,description,status) values(?,?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);

        pstmt.setString(1,brandName);
        pstmt.setString(2,companyName);
        pstmt.setInt(3,ordered);
        pstmt.setString(4,description);
        pstmt.setInt(5,status);
        int i = pstmt.executeUpdate();


        System.out.println(i>0);
        pstmt.close();
        conn.close();
    }

    @Test
    public void TestUP() throws Exception {

        String brandName = "theshy";
        String companyName = "zmh";
        int ordered = 1000;
        String description = "go ahead";
        int status = 1;
        int id =10;

        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();

        String sql = "Update tb_brand set" +
                " brand_name = ?," +
                "company_name = ?," +
                "ordered = ?," +
                "description = ?," +
                "status = ? where id =?";
        PreparedStatement pstmt = conn.prepareStatement(sql);

        pstmt.setString(1,brandName);
        pstmt.setString(2,companyName);
        pstmt.setInt(3,ordered);
        pstmt.setString(4,description);
        pstmt.setInt(5,status);
        pstmt.setInt(6,id);
        int i = pstmt.executeUpdate();


        System.out.println(i>0);
        pstmt.close();
        conn.close();
    }

    @Test
    public void TestDel() throws Exception {

        int id = 10;

        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();

        String sql = "Delete from tb_brand where id =?";
        PreparedStatement pstmt = conn.prepareStatement(sql);

        pstmt.setInt(1,id);
        int i = pstmt.executeUpdate();


        System.out.println(i>0);
        pstmt.close();
        conn.close();
    }

}
